import axios from 'axios'
import qs from 'qs'

export default {
    namespaced: true,
    actions: {
        getUser: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("getUser", params);
        },
        getAllUser: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data.users);
            }
            commit("getAllUser", params);
        },
        addUser: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("addUser", params);
        },
        updateUser: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("updateUser", params);
        },
        deleteUser: function ({commit},params) {
            let {callback} = params;

            params.onget = ({data}) =>{
                callback(data.success);
            }

            commit("deleteUser",params)
        },
    },
    mutations: {
        getUser: function (state, {id, onget}) {
            axios.get(`/admin/user/${id}`).then(data => {
                onget(data);
            })
        },
        getAllUser: function (state, {username, userIds,page,size, onget}) {
            axios.get(`/admin/user`, {
                params: {
                    username,
                    userIds,page,size
                }
            }).then(data => {
                onget(data);
            })
        },
        addUser: function (state, {name, userIds, onget}) {
            axios.post(`/admin/user`, qs.stringify({
                name,
                userIds
            })).then(data => {
                onget(data);
            })
        },
        updateUser: function (state, {id, name, userIds, onget}) {
            axios.put(`/admin/user`, qs.stringify({
                id,
                name,
                userIds
            })).then(data => {
                onget(data);
            })
        },
        deleteUser: function (state, {id, onget}) {
            axios.delete(`/admin/user`,{params:{id}}).then(data => {
                onget(data);
            });
        },
    }

}